package com.education.wisdom.controller;


import com.education.common.config.WebSocketServer;
import com.education.common.utils.ApiReturnObject;
import com.education.wisdom.pojo.Message;
import com.education.wisdom.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/web/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/list")
    public ApiReturnObject list(Message message, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Message> messageList = messageService.selectMessageList(message);
        //把该用户消息修改为已读
        Message me = new Message();
        me.setUserId(message.getUserId());
        me.setIsRead("0");
        messageService.updateMessage(me);
        return new ApiReturnObject("200", "操作成功", new PageInfo<Message>(messageList));
    }


    @GetMapping("/selectCount")
    public Integer selectCount(String userId) {
        Integer count = messageService.selectCount(userId);
        return count;
    }

    @GetMapping("/selectMessageCount")
    public void selectMessageCount(String userId) {
        Integer count = messageService.selectCount(userId);
        try {
            WebSocketServer.sendInfo(count.toString(), userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
